Borland Online And The Cobb Group Present:


August, 1995 - Vol. 2 No. 8

Using dialog boxes as main windows

For many applications, you may not need to create a main application window, a menu bar, and menu items­­instead, a single dialog box may be an appropriate user interface. The OpenHelp application, which manages navigation in the Borland C++ 4.5 online help system, uses this approach, as shown in Figure A.


Figure A - The OpenHelp application uses a dialog box as its main window.

In previous versions of the ObjectWindows Library (OWL), you could specify a dialog box as the application's main window. However, Microsoft is now discouraging this practice. Accordingly, the latest versions of the OWL class library force you to use a frame window as the application's main window.

In this article, we'll show how you can create a main window that's a frame window but that behaves like a dialog box. You'll do this by creating a special type of dialog box window that's also a client window.

A touch of TDialog class

Before you can create a frame window that will act like a dialog box, you need to derive a new class from the OWL TDialog class. In this new class, you'll create an OWL interface object for the important controls in the dialog box resource, and you'll define the event-handling functions that will give the application dialog box functionality.

For example, if you want the application to respond to pushbutton clicks, you'll need to define event-handlers for the EV_BN_CLICKED message. For that particular message, you can specify a different event-handler for each pushbutton's resource ID.

Once you've derived the new dialog box class, you'll want to create a corresponding dialog box resource that includes resources for the buttons, list boxes, edit fields, or other controls that will appear in the dialog box. You can create this resource file either manually, by using a text editor, or graphically, by using the Resource Workshop application that ships with Borland C++.

However, you don't want to use a standard dialog box resource to initialize a main window in a dialog box application. This is because the adornments of a standard dialog box won't be visible when you display it as a client window. Instead, you'll decorate the frame window so that it resembles the border of a standard dialog box.

If you use the Resource Workshop to create the dialog box, you'll want to create a child dialog box instead of a pop-up or overlapped dialog box. (A child dialog box doesn't have any of the standard adornments.)

As usual, you'll need to derive a new application class from the OWL TApplication class. In this class, you'll need to override the InitMainWindow() member function to create a TFrameWindow object that contains an instance of your new dialog box class. By the way, to force the frame window to conform to the size of the client window (that is, the dialog box) instead of the reverse, you'll need to specify a value of TRUE for the shrinkToClient parameter of the TFrameWindow constructor.

An open dialog

Now let's create a simple application that exits Windows. If you regularly keep Program Manager minimized, you can use this simple application instead of maximizing Program Manager, choosing Exit from the File menu, and clicking OK in the confirmation dialog box.

To begin, launch the Borland C++ Integrated Development Environment (IDE). When the IDE's main window appears, choose New Project... from the Project menu. In the New Target dialog box, enter \dlgaswnd\dlgaswnd.ide in the Project Path And Name entry field. Next, choose Application [.exe] from the Target Type list box and Windows 3.x (16) from the Platform combo box. Then select the OWL check box in the Standard Libraries section and click OK.

When the project window appears, click dlgaswnd [.def] and press [Del]. In the confirmation dialog box that appears, click OK to delete this node from the project. (We'll use the default definitions file instead.)

Next, double-click dlgaswnd [.cpp] in the project window. When the editing window for this file appears, enter the code from Listing A. When you finish, choose Save from the File menu.


Listing A: DLGASWND.CPP

#include <owl\owlpch.h>

class TDialogWindow : public TDialog
{
 public:
  TDialogWindow() :
	 TDialog(0, 1)
  { ExitButton = new
		TButton(this, 101);
	 CancelButton = new
		TButton(this, 102);}
 protected:
  TButton* ExitButton;
  TButton* CancelButton;

  void DoExitButton()
  { ExitWindows(0L, 0); }

  void DoCancelButton()
  { CloseWindow(); }

  DECLARE_RESPONSE_TABLE(TDialogWindow);
};

DEFINE_RESPONSE_TABLE1(TDialogWindow, TDialog)
 EV_BN_CLICKED(101, DoExitButton),
 EV_BN_CLICKED(102, DoCancelButton),
END_RESPONSE_TABLE;

class DialogApplication : public TApplication
{
 public:
  void InitMainWindow()
  { BOOL ShrinkToClient = TRUE;
	 SetMainWindow(new TFrameWindow(0,
		"Exit Windows?",
		new TDialogWindow(),
		ShrinkToClient));
	 GetMainWindow()->Attr.Style &=
		~(WS_THICKFRAME | WS_MAXIMIZEBOX);
  }
};

int OwlMain(int, char**)
{ return DialogApplication().Run(); }

Next, right-click dlgaswnd [.rc] in the project window, choose View from the pop-up menu, and then choose TextEdit from the View submenu. When the editing window for the resource file appears, enter the code from Listing B. When you've finished entering this resource definition, choose Save from the File menu.


Listing B: DLGASWND.RC

1 DIALOG 6, 15, 161, 70
STYLE WS_CHILD | WS_VISIBLE
FONT 8, "MS Sans Serif"
{
 PUSHBUTTON "Exit", 101, 19, 28, 50, 14
 PUSHBUTTON "Cancel", 102, 92, 28, 50, 14
}

To build and run the application, choose Run from the Debug menu. When the IDE finishes compiling and linking the application, its main window will appear, as shown in Figure B.


Figure B - The Exit Windows? window simulates a standard dialog box using a frame window and a client dialog box.

If you click Exit or press [Enter], the application will exit Windows. If, instead, you click Cancel or press [Esc], the application will exit, and you'll return to the IDE.

Conclusion

Many simple applications don't need an elaborate user interface. You can use the technique we've shown here to create an OWL application that displays a dialog box as its main window.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.